package kiosk;
/**
 * Importeringer
 */
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JButton;
import javax.swing.JOptionPane;

import java.util.ArrayList;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.BorderLayout;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;

import java.io.*;
/**
 * Klasse:  GUIKiosk
 * @author  RK
 * @version 1.0
 * -------------------------
 * Sist endret: 6 April 2010
 */
public class GUIKiosk extends JFrame {

    private Admin admin;
    private String s;
    private GUIKurv kurv;
    private Salg salget;

    public GUIKiosk() throws IOException
    {
        this.s = "";

        
        this.admin = new Admin();
        this.admin.nyttProdukt("test", "1234", 20, 20, 20, 20);
        this.admin.nyttProdukt("asdf", "12", 30, 30, 30, 30);
        this.lagre();
         

        this.hent();
        this.initKomponenter();
    }

    public void lagre() throws IOException
    {
        try
        {
            FileOutputStream output = new FileOutputStream("lister.ser");
            ObjectOutputStream out = new ObjectOutputStream(output);
            out.writeObject(this.admin);
            output.close();
            out.close();
            JOptionPane.showMessageDialog(null, "Data lagret");
        }
        catch (Exception e)
        {
            JOptionPane.showMessageDialog(null, e);
        }
    }

    public void hent() throws IOException
    {
        try
        {
            FileInputStream input = new FileInputStream("lister.ser");
            ObjectInputStream in = new ObjectInputStream(input);
            this.admin = (Admin) in.readObject();
            input.close();
            in.close();
            JOptionPane.showMessageDialog(null, "Data hentet");
        }
        catch (Exception e)
        {
            JOptionPane.showMessageDialog(null, e);
        }
    }

    /**
     * initKomponenter
     * ----------------------------------------
     * Lager alle komponentene som vises i vinduet
     */
    private void initKomponenter() throws IOException
    {
        // Innstillinger for vinduet
        this.setDefaultCloseOperation(EXIT_ON_CLOSE);
        this.setSize(800, 300);
        this.setPreferredSize(new Dimension(800, 300));
        this.setLocationRelativeTo(null);        
        this.addKeyListener(new TasteLytter());
        this.setLayout(new BorderLayout());
        this.pack();

        // Komponenter til vinduet
        kurv = new GUIKurv();
        kurv.setPreferredSize(new Dimension(760, 200));
        kurv.setFocusable(false);
        this.add(kurv, BorderLayout.NORTH);

        // Knappepanel
        JPanel kp = new JPanel();
        kp.setLayout(new FlowLayout());
        JButton btn = new JButton("Fjern");
        btn.setFocusable(false);
        btn.setPreferredSize(new Dimension(100, 20));
        btn.addActionListener(new ActionListener() {
           public void actionPerformed(ActionEvent e)
           {
               kurv.fjernProduktFraKurv();
           }
        });

        kp.add(btn);
        btn = new JButton("Tøm kurv");
        btn.setFocusable(false);
        btn.setPreferredSize(new Dimension(100, 20));
        btn.addActionListener(new ActionListener() {
           public void actionPerformed(ActionEvent e)
           {
               kurv.tømKurv();
           }
        });
        kp.add(btn);

        btn = new JButton("Fullfør salg");
        btn.setFocusable(false);
        btn.setPreferredSize(new Dimension(100, 20));
        btn.addActionListener(new ActionListener() {
           public void actionPerformed(ActionEvent e)
           {
               if (kurv.getNumRows() != 0)
               {
                    admin.addSalg(kurv.getPris(), 0, "lala", kurv.getKurv());
                    kurv.fullførSalg();
                    oppdaterProdukter(kurv.getKurv());
                    try
                    {                        
                        lagre();
                    }
                    catch (Exception a)
                    {

                    }
               }
               else
               {
                   JOptionPane.showMessageDialog(null, "Ingenting i kurven");
               }
           }
        });
        kp.add(btn);
        this.add(kp, BorderLayout.SOUTH);
        this.setVisible(true);


    }

    public void oppdaterProdukter(ProduktListe prod)
    {
        ArrayList<Produkt> produkter = prod.getProdukter();
        for (int i = 0; i < prod.getAntallProdukter(); i++)
        {
            Produkt p = produkter.get(i);
            Produkt g = admin.getProdukt(p.getStrekkode());
            g.økAntallSolgt(p.getAntallIKurv());
            admin.endreProdukt(g);
        }
    }

    /**
     * Indre klasse
     * -------------------------
     * Klasse:  TasteLytter
     * @author  RK
     * @version 1.0
     * -------------------------
     * Sist endret: 30 Mars 2010
     */
    private class TasteLytter implements KeyListener {

        public void keyPressed(KeyEvent k)
        {
            if (k.getKeyChar() == KeyEvent.VK_ENTER)
            {
                if (admin.finnProdukt(s))
                {
                    kurv.addProduktTilKurv(admin.getProdukt(s));
                    s = "";
                }
                else
                {
                    JOptionPane.showMessageDialog(null, "Fant ikke produktet");
                    s = "";
                }
            }
            else
            {
                s += k.getKeyChar();
            }
        }

        public void keyReleased(KeyEvent k)
        {

        }

        public void keyTyped(KeyEvent k)
        {

        }
    }

    /**
     * main
     * --------------------------------------------
     * Kjører programmet
     *
     * @param args
     */
    public static void main(String[] args) throws IOException
    {
        GUIKiosk k = new GUIKiosk();
    }
}
